package com.shop.Service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.Service.BrandService;
import com.shop.mapper.GoodMapper;
import com.shop.pojo.Brand;
import com.shop.pojo.Message;
import com.shop.mapper.BrandMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {
    @Override
    public Message<String> addBrand(Brand brand) throws Exception {
        Message<String> message = new Message<>();
        InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        int i = mapper.save(brand);
        if (i>0){
            message.setMessage("添加成功");
            message.setData("添加成功");
            sqlSession.commit();
        }else {
            message.setMessage("添加失败");
        }
        return message;
    }
    @Override
    public Message<PageInfo<Brand>> selectAllBrand(String queryInfo, int pageSize, int pageNum) throws IOException{
        Message<PageInfo<Brand>> message = new Message<>();
        InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        PageHelper.startPage(pageNum,pageSize);
        if (queryInfo==null){
            queryInfo="";
        }
        List<Brand> brandList =  mapper.selectBrand(queryInfo);
        message.setMessage("获取成功");
        PageInfo<Brand> pageInfo = new PageInfo<Brand>(brandList);
        message.setData(pageInfo);
        return message;
    }

    @Override
    public Message<String> deleteBrand(int pid) throws Exception {
        Message<String> message =new Message<>();
        InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        int i = mapper.deleteBrand(pid);
        if (i>0){
            message.setMessage("删除成功");
            message.setData("删除成功");
        }else {
            message.setMessage("删除失败");
        }
        return message;
    }

    @Override
    public Message<String> updateBrand(int pid, String pname, String pimgurl) throws IOException {
        Message<String> message =new Message<>();
        InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        Brand brand = new Brand(pid,pname,pimgurl);
        int i = mapper.updateBrand(brand);
        if (i>0){
            sqlSession.commit();
            message.setMessage("修改成功");
            message.setData("修改成功");
        }else {
            message.setMessage("修改失败");
        }
        return message;
    }

    @Override
    public Message<List<Brand>> selectAll() throws IOException {
        Message<List<Brand>> message =new Message<>();
        InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        List<Brand> brandList = mapper.selectAll();
        message.setMessage("获取成功");
        message.setData(brandList);
        return message;
    }
    @Override
    public Message<Integer> selectCount() throws Exception{
        Message<Integer> message = new Message<>();
        InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        int num = mapper.selectCount();
        message.setMessage("获取成功");
        message.setData(num);
        return message;
    }

    @Override
    public Message<Brand> selectById(int pid) throws Exception {
        Message<Brand> message = new Message<>();
        InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        Brand brand = new Brand();
        brand = mapper.selectById(pid);
        message.setData(brand);
        message.setMessage("获取成功");
        return message;
    }


}
